﻿@using Coldairarrow.Entity.Base_SysManage;
@using Coldairarrow.Util;
@using Coldairarrow.Business.Base_SysManage;

@{
    Layout = "~/Views/Shared/_Layout_Form.cshtml";

    var obj = (Base_User)Model;
    object roleIds = "[]";
    if (!obj.Id.IsNullOrEmpty())
    {
        roleIds = Html.Raw(Base_UserBusiness.GetUserRoleIds(obj.UserId).ToJson());
    }
    string str = obj.ToJson();
    var objStr = Html.Raw(obj.ToJson());
}
<div style="padding:15px;padding-right:45px;">
    <form id="form" class="form-horizontal" role="form">
        <div class="form-group form-group-sm">
            <label class="col-sm-2 control-label">用户名</label>
            <div class="col-sm-5">
                <input name="UserName" value="@obj.UserName" type="text" class="form-control" required>
                <div class="help-block with-errors"></div>
            </div>
        </div>
        <div class="form-group form-group-sm">
            <label class="col-sm-2 control-label">密码</label>
            <div class="col-sm-5">
                <input name="Pwd" type="password" class="form-control">
                <div class="help-block with-errors"></div>
            </div>
        </div>
        <div class="form-group form-group-sm">
            <label class="col-sm-2 control-label">姓名</label>
            <div class="col-sm-5">
                <input name="RealName" value="@obj.RealName" type="text" class="form-control" required>
                <div class="help-block with-errors"></div>
            </div>
        </div>
        <div class="form-group form-group-sm">
            <label class="col-sm-2 control-label">性别</label>
            <div class="col-sm-5">
                <select id="Sex" name="Sex" class="selectpicker form-control" data-style="btn-default btn-sm">
                    <option value="1">男</option>
                    <option value="0">女</option>
                </select>
                <div class="help-block with-errors"></div>
            </div>
        </div>
        <div class="form-group form-group-sm" id="container" style="position:relative">
            <label class="col-sm-2 control-label">出生日期</label>
            <div class="col-sm-5" style="position:relative">
                <input id="Birthday" name="Birthday" type="text" value="@(obj.Birthday?.ToString("yyyy-MM-dd"))" data-date-format="yyyy-mm-dd" class="form-control">
            </div>
        </div>
        <div class="form-group form-group-sm">
            <label class="col-sm-2 control-label">角色</label>
            <div class="col-sm-5">
                <select id="roleList" class="selectpicker form-control" name="roleList" multiple data-style="btn-default btn-sm"></select>
                <div class="help-block with-errors"></div>
            </div>
        </div>
        <div class="form-group">
            <button id="submit" type="submit" class="hidden">提交</button>
        </div>
    </form>
</div>
<script>
        var theEntity = @objStr;
        var roleIds =@roleIds;

        $(function () {
            initEvent();
        });

        //事件绑定
        function initEvent() {
            $('#Sex').selectpicker({
                value: theEntity.Sex
            });
            $('#Birthday').datetimepicker({
                language: 'zh-CN',
                minView: 2//日期选择
            });
            $('#roleList').selectpicker({
                url: '/Base_SysManage/Base_SysRole/GetDataList_NoPagin',
                value: roleIds,
                valueField: 'RoleId',
                textField: 'RoleName',
            });
            //表单校验
            $('#form').validator().on('submit', function (e) {
                //校验成功
                if (!e.isDefaultPrevented()) {
                    e.preventDefault();

                    var values = $('#form').getValues();
                    $.extend(theEntity, values);
                    theEntity['RoleIdList'] = JSON.stringify(theEntity['roleList'] || []);
                    loading();
                    $.postJSON('/Base_SysManage/Base_User/SaveData', theEntity, function (resJson) {
                        loading(false);

                        if (resJson.Success) {
                            parent.$('#dataTable').bootstrapTable('refresh');
                            parent.dialogSuccess();
                            dialogClose();
                        }
                        else {
                            dialogError(resJson.Msg);
                        }
                    });
                }
            })
        }

        //提交表单
        function submitForm() {
            $('#submit').trigger('click');
        }
</script>
